<?
class ccdsDefaultPlugin
	extends ccdsBackendPlugin
{
	public $name = "ccdsDefaultPlugin";

	private $BackUserPlugins = array();

	function __construct()
	{
		parent::__construct();

		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "openLogin", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "loginUser", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "initApp", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "initNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "updateNavi", $this->name );


		$this->xajax->register( XAJAX_CCDS_FUNCTION, "getNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "updateNavi", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "changeProjectLanguage", $this->name );

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "logout", $this->name );


		$this->xajax->register( XAJAX_CCDS_FUNCTION, "killSession", $this->name );
		$this->xajax->register( XAJAX_CCDS_FUNCTION, "debugSession", $this->name );



	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function killSession() {
		$objResponse = new ccdsResponse();
		
		$_SESSION['CMS'] = array();
		session_write_close();
		return $objResponse;
	}

	function debugSession() {
		$objResponse = new ccdsResponse();
		
		$objResponse->call("CMS.debugMessage",print_r($_SESSION,true));
		
		return $objResponse;
	}




	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function loginUser( $aFormValues )
	{
		$objResponse = new ccdsResponse();

		$query = "SELECT * FROM user LEFT OUTER JOIN languages ON user_editor_lang_id = lang_id WHERE user_login_name='".$aFormValues['user_loginname']."' LIMIT 1";
		$res = $this->ccdSQL->query( $query );

		$configvars = $this->smarty->get_config_vars();
		
		if ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			if ( $row['user_del'] == 1 )
			{
				$objResponse->assign( "DivLoginFormError", "innerHTML",$configvars['Global_suspended_login'] );
			}elseif ( $row['user_login_pass'] == md5($aFormValues['user_loginpass']) )
			{
				$this->tools->init_session($row);
				$objResponse->loadcommands( $this->initApp( array ()) );
				$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/arm.mp3');");
				if (1 == $aFormValues['user_cookielogin']) {
					$cookie_value = MD5(time().$row['user_id']);
					$this->tools->set_cookie_fix_domain("CCDS3_USER", $cookie_value, time()+(180*3600*24),"/",$_SERVER['HTTP_HOST']);
				} else $cookie_value='';
				$this->ccdSQL->query("UPDATE user SET user_cookie='".$cookie_value."' WHERE user_id='".$row['user_id']."' LIMIT 1");

			}else
			{
				$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/disarm.mp3');");
				$objResponse->assign( "DivLoginFormError","style.display","block");
				$objResponse->assign( "DivLoginFormError", "innerHTML", $configvars['Global_bad_login'] );
			}
		}else
		{
			$objResponse->assign( "DivLoginFormError","style.display","block");
			$objResponse->assign( "DivLoginFormError", "innerHTML", $configvars['Global_bad_login'] );
		}
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */


	function logout() {
	  $_SESSION['CMS']= array();
	  unset($_SESSION['CMS']);
	  $objResponse = new ccdsResponse();
	  $objResponse->clsTabManager->destroy('topTab');

	  $objResponse->script("CMS.clear_tiny_instances();");
	  $objResponse->assign("CMS","innerHTML","");
	  $objResponse->loadcommands($this->openLogin(array()));
		$objResponse->script("document.fsd.SetVariable('_src', '/ccds_library/flashplayer/disarm.mp3');");

		$this->tools->set_cookie_fix_domain("CCDS3_USER", '', time()+(180*3600*24),"/",$_SERVER['HTTP_HOST']);

	  return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function openLogin( $aFormValues = false )
	{
		$objResponse = new ccdsResponse();
		$this->smarty->assign("ccdsClass",$this->name);
		$loginForm = $this->smarty->fetch( $this->tpl_dir."login.tpl" );
		
		
		$objResponse->assign( "CMS", "innerHTML", $loginForm );

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function initApp()
	{
		$objResponse = new ccdsResponse();
		
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/jquery-ui-1.7.2.custom.min.js');

		//$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'tinymce/jscripts/tiny_mce/tiny_mce.js');

//		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/i18n/ui.datepicker-de.js');


		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.js?x='.time());
		$objResponse->waitFor("typeof $.xWindow == 'function'",200);
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.dialog.ext.js?x='.time());

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/tablednd/jquery.tablednd.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/xTree/jquery.xTree.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.typeListener.js');

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.filemanager.js');

		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/imageEditor/jquery.imageEditor.js?x='.time());
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/templating.js?x='.time());
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/tooltip/jquery.tooltip.min.js');
		$objResponse->includeScriptOnce($this->config->get('CCDS_DIR').'js/comet.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/jgcharts/jgcharts.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'codemirror/js/codemirror.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/jgrow/jquery.jgrowl_compressed.js');
		$objResponse->includeScriptOnce($this->config->get('LIBRARY_WEB').'jquery/plugins/checkboxes/jquery.checkboxes.min.js');

		$objResponse->waitFor("typeof $.xTree == 'function'",200);

		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/xTree/jquery.xTree.css');
		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/xWindow/jquery.xWindow.css');

		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/filemanager/jquery.filemanager.css');
		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/imageEditor/jquery.imageEditor.css');
		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/tooltip/jquery.tooltip.css');
		$objResponse->includeCSS($this->config->get('LIBRARY_WEB').'jquery/plugins/jgrow/jquery.jgrowl.css');

		$objResponse->includeScriptOnce($this->config->get('CCDS_DIR').'lang/tpl_lang/de.js');





		$home_navi=array();
		$foo = array(&$home_navi);
		$this->modules->call('home_navi',$foo);
		$this->smarty->assign('home_navi',$home_navi);
		
		$admin_navi = array();
		$foo = array(&$admin_navi);
		$this->modules->call('admin_navi',$foo);
		$this->smarty->assign('admin_navi',$admin_navi);


		$sHtml = array();
		$foo = array(&$sHtml);
		$this->modules->call('INIT_APP',$foo);
		$this->smarty->assign('templates_callback',implode("\n",$sHtml));

		$sHtml = $this->smarty->fetch( $this->tpl_dir.'layout.tpl' );
		$objResponse->assign( "CMS", "innerHTML", $sHtml );

		$objResponse->waitFor("typeof xajax.$('CMS_layout_append') == 'object'",300);

		$config = array
		(
			"tabPanel" => 'ContentEditorNavi',
			"tabContent" => 'ContentArea',
			"cssClass" => 'topTab',
			"closeable_all" => true
		);


		$objResponse->clsTabManager->create( 'topTab', $config );

		$objResponse->loadcommands( $this->initNavi() );

		$sHtml = array();
		$foo = array(&$objResponse);
		$this->modules->call('INIT_APP_TABS',$foo);

	return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */
	function initNavi()
	{
		
		$objResponse = new ccdsResponse();
		$projects = array ();

		if ( $_SESSION['CMS']['user_level'] < 1000 )
		{
			$query = "SELECT * FROM
	               projects
	               LEFT JOIN
	               permissions_project_user ON (project_user_key_project_id=project_id)
	               WHERE
	               project_user_key_user_id='".$_SESSION['CMS']['user_id']."'
	               AND
	               project_user_read = 1
	               ORDER by project_name";
		}
		else
		{
			$query = "SELECT * FROM
	               projects
	               WHERE
	               1
	               ORDER by project_name";
		}
		$res = $this->ccdSQL->query( $query );

		$languages=array();
	
		if (!is_array($_SESSION['CMS']['lang'])) $_SESSION['CMS']['lang'] = array();
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) )
		{
			$projects[] = $row;
		}
		$this->smarty->assign( "projects", $projects );
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_navi_container.tpl" );
		$objResponse->assign( "divprojects", "innerHTML", $sHtml );		

		foreach ($projects as $project) {
			$objResponse->loadcommands( $this->updateProjectNavi($project) );
		}

		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function getNaviEntries( $aFormValues = array() )
	{
		if ( $project_id != false )
		{
			$project_query = " AND obj1.obj_key_project_id='".$project_id."' ";
		}
		else
		{
			$project_query = "";
		}
		$query = "
			SELECT 
				obj1.*,
				class_name,
				class_dir,
				ext_dir,
				project_dir,
				objp_key_obj_id
			FROM 
					obj_index as obj1  
				STRAIGHT_JOIN classes ON
					(obj1.obj_key_class_id = class_id)
				STRAIGHT_JOIN extensions ON
					(ext_id = class_key_ext_id)
				STRAIGHT_JOIN projects ON
					(project_id = ext_key_project_id)
			LEFT OUTER JOIN 
				obj_index_public ON 
				(objp_key_obj_id=obj_id AND objp_key_lang_id=".$_SESSION['CMS']['lang'][$aFormValues['project_id']].")
			";
		$query .= "
			WHERE 
				obj1.obj_parent_obj_id = '".$aFormValues['obj_parent_obj_id']."' 
				AND obj1.obj_deleted = 0 
				".$project_query."
			ORDER BY 
				obj1.obj_position
		";	
		$res = $this->ccdSQL->query($query);
		
		
		$results = array();
		while ( $row = $this->ccdSQL->fetch_assoc( $res ) ) 
		{

			if ( 1000 > $_SESSION['CMS']['user_level']) {
				$permissons = $this->tools->PermissionsObj2UserGet( $_SESSION['CMS']['user_id'], $row['obj_id'], $_SESSION['CMS']['user_level'] );				
				if (1 <> $permissons['read']) continue;
			}


			if ( $row['obj_link_obj_id'] > 0 )
			{
				$query = "SELECT * FROM obj_index 
										STRAIGHT_JOIN classes ON
											(obj_key_class_id = class_id)
										STRAIGHT_JOIN extensions ON
											(ext_id = class_key_ext_id)
										STRAIGHT_JOIN projects ON
											(project_id = ext_key_project_id)											
										WHERE obj_id='".$row['obj_link_obj_id']."'
										
					";
				$res2 = $this->ccdSQL->query( $query );
				$link_obj = $this->ccdSQL->fetch_assoc( $res2 );
			
				$link_obj['obj_id'] = $row['obj_id'];
			
				$row = $link_obj;
			}
			

			if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
			{
				$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
			}
			else
			{
				$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
			}
			$name = $this->tools->getObjectName( $row['obj_id'],$_SESSION['CMS']['lang'][$row['obj_key_project_id']], true );
			$row = array_merge( $row, $name );
			$row['icon'] = $icon_src;

			$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_parent_obj_id='".$row['obj_id']."' AND obj_deleted='0'";
			$res2 = $this->ccdSQL->query( $query );
			$anzahl = $this->ccdSQL->fetch_assoc( $res2 );
			$count = $anzahl['anzahl'];


			$row['childs'] = $count;
			$results[] = $row;
		}

		return $results;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */



	/* ---------------------------------------------------------------------------------------------------------------------------------- */


	function getNavi( $aFormValues = array() )
	{

		$objModuleManager = ccdsModuleManager::getInstance();

		$objResponse = new ccdsResponse();
		
		
		$results = $this->getNaviEntries( $aFormValues );
		
		
		$elements = array();

		foreach($results as $obj) {

			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;

			$add_obj = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'].' ('.$obj['childs'].')',
				"icon" => $obj['icon'],
				"disabled" => $disabled,
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name'])
			
			);
			;
			if ($class = $objModuleManager->getBackendUserPlugin($obj['obj_key_class_id']))
			{
				$add_obj['onNameClick'] = $this->tools->strip("function() {
					xajax.request(
						{ccdsfun: 'open', 
						ccdsclass: 'ccds".$class['class_name']."',
						ccdsclass_id:".$obj['obj_key_class_id']."
						}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});
					}");

			} else {
				$add_obj['onNameClick'] = $this->tools->strip("function() {
					xajax.request(
						{ccdsfun: 'ContentEditorOpen', 
						ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});
					}");
			}
			
			if ($obj['childs'] > 0) 
			{
				$add_obj['folder'] = true;
				$add_obj['onOpen'] = "function() {xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsDefaultPlugin'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});}";
			}
			$elements[] = $add_obj;
		}

		$objResponse->script($this->tools->strip("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
			array(
				"parent" => array("id" => "navi_obj_".$aFormValues['obj_parent_obj_id']),
				"elements" => $elements
			)
		).")"));

		$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('open',{id:'navi_obj_".$aFormValues['obj_parent_obj_id']."'})");
		
		return $objResponse;
	}

	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateNaviEntry( $aFormValues = array() , $childs = false )
	{
		$objResponse = new ccdsResponse();
		
		if ( "" == $aFormValues['project_id'] ) 
		{
			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($aFormValues['obj_parent_obj_id'])."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];			
		}
		
		if ( 0 == $aFormValues['obj_parent_obj_id']) $aFormValues['parent_id'] = "project_navi_root_".$aFormValues['project_id'];

		if ( $project['project_id'] != false )
		{
			$project_query = " AND obj1.obj_key_project_id='".$project['project_id']."' ";
		}
		else
		{
			$project_query = "";
		}
		$query = "
			SELECT 
				obj1.*,
				class_name,
				class_dir,
				ext_dir,
				project_dir
			FROM 
					obj_index as obj1  
				STRAIGHT_JOIN classes ON
					(obj1.obj_key_class_id = class_id)
				STRAIGHT_JOIN extensions ON
					(ext_id = class_key_ext_id)
				STRAIGHT_JOIN projects ON
					(project_id = ext_key_project_id)
			LEFT OUTER JOIN 
				obj_index_public ON 
				(objp_key_obj_id=obj_id AND objp_key_lang_id=".$_SESSION['CMS']['lang'][$aFormValues['project_id']].")
			WHERE 
				obj1.obj_id = '".$aFormValues['obj_parent_obj_id']."' 
				AND obj1.obj_deleted = 0 
				".$project_query."
			ORDER BY 
				obj1.obj_position
		";	
		

		$res = $this->ccdSQL->query($query);
		$results = array();
		if ($row = $this->ccdSQL->fetch_assoc( $res )) {

			if ( $row['obj_link_obj_id'] > 0 )
			{
				$query = "SELECT * FROM obj_index 
										STRAIGHT_JOIN classes ON
											(obj_key_class_id = class_id)
										STRAIGHT_JOIN extensions ON
											(ext_id = class_key_ext_id)
										STRAIGHT_JOIN projects ON
											(project_id = ext_key_project_id)											
										WHERE obj_id='".$row['obj_link_obj_id']."'
										
					";
				$res2 = $this->ccdSQL->query( $query );
				$link_obj = $this->ccdSQL->fetch_assoc( $res2 );
			
				$link_obj['obj_id'] = $row['obj_id'];
			
				$row = $link_obj;
			}
			
	
			if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
			{
				$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
			}
			else
			{
				$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/page.png";
			}
			$name = $this->tools->getObjectName( $row['obj_id'],$_SESSION['CMS']['lang'][$row['obj_key_project_id']], true );
			$row = array_merge( $row, $name );
			$row['icon'] = $icon_src;
	
			$query = "SELECT COUNT(*) as anzahl FROM obj_index WHERE obj_parent_obj_id='".$row['obj_id']."' AND obj_deleted='0'";
			$res2 = $this->ccdSQL->query( $query );
			$anzahl = $this->ccdSQL->fetch_assoc( $res2 );
			$count = $anzahl['anzahl'];
	
			$row['childs'] = $count;
	
			$update_obj = array(
				"id" => "navi_obj_".$row['obj_id']
				,"name" => $row['obj_name_name']
			);
	 		
	
			if (!$childs) 
			{
				$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
					array(
						"parent" => $update_obj
					)
				).")");
				return $objResponse;
			}
	
			$results = $this->getNaviEntries( $aFormValues );
			$elements = array();
	
			$objModuleManager = ccdsModuleManager::getInstance();
	
			foreach($results as $obj) {
				$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
				$add_obj = array(
					"id" => "navi_obj_".$obj['obj_id'],
					"name" => $obj['obj_name_name'],
					"icon" => $obj['icon'],
					"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
					"disabled" => $disabled
				);
	
				if ($class = $objModuleManager->getBackendUserPlugin($obj['obj_key_class_id']))
				{
					$add_obj['onNameClick'] = $this->tools->strip("function() {
						xajax.request(
							{ccdsfun: 'open', 
							ccdsclass: 'ccds".$class['class_name']."',
							ccdscomet:1,
							ccdsclass_id:".$obj['obj_key_class_id']."
							}, {comet:1,parameters: [{obj_id: '".$obj['obj_id']."'}]});
						}");
	
				} else {
					$add_obj['onNameClick'] = $this->tools->strip("function() {
						xajax.request(
							{ccdsfun: 'ContentEditorOpen', 
							ccdsclass: 'ccdsContentEditor',
							ccdscomet:1}, {comet:1,parameters: [{obj_id: '".$obj['obj_id']."'}]});
						}");
				}
	
	
				if ($obj['childs'] > 0) 
				{
					$add_obj['folder'] = true;
					$add_obj['onOpen'] = "function() {
						xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsDefaultPlugin'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});
					}";
				}
				$elements[] = $add_obj;
			}
	
			$objResponse->script("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
				array(
					"parent" => $update_obj
					,"elements" => $elements
				)
			).")");
		}


		return $objResponse;
	}
	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateNavi( $aFormValues = array() )
	{
		$objResponse = new ccdsResponse();
		
		if ( "" == $aFormValues['project_id'] ) 
		{

			$query = "SELECT obj_key_project_id FROM obj_index WHERE obj_id='".addslashes($aFormValues['obj_parent_obj_id'])."' LIMIT 1";
			$res = $this->ccdSQL->query( $query );
			$project = $this->ccdSQL->fetch_assoc( $res );
			$aFormValues['project_id'] = $project['obj_key_project_id'];			
			
		}
		
		if ( 0 == $aFormValues['obj_parent_obj_id']) $aFormValues['parent_id'] = "project_navi_root_".$aFormValues['project_id'];
		
		
		$results = $this->getNaviEntries( $aFormValues );
		$elements = array();

		$objModuleManager = ccdsModuleManager::getInstance();

		foreach($results as $obj) {
			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
			$add_obj = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'].' ('.$obj['childs'].')',
				"icon" => $obj['icon'],
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
				"disabled" => $disabled
			);


			if ($class = $objModuleManager->getBackendUserPlugin($obj['obj_key_class_id']))
			{
				$add_obj['onNameClick'] = $this->tools->strip("function() {
					xajax.request(
						{ccdsfun: 'open', 
						ccdsclass: 'ccds".$class['class_name']."',
						ccdscomet:1,
						ccdsclass_id:".$obj['obj_key_class_id']."
						}, {comet:1,parameters: [{obj_id: '".$obj['obj_id']."'}]});
					}");

			} else {
				$add_obj['onNameClick'] = $this->tools->strip("function() {
					xajax.request(
						{ccdsfun: 'ContentEditorOpen', 
						ccdsclass: 'ccdsContentEditor',
						ccdscomet:1}, {comet:1,parameters: [{obj_id: '".$obj['obj_id']."'}]});
					}");
			}


			if ($obj['childs'] > 0) 
			{
				$add_obj['folder'] = true;
				$add_obj['onOpen'] = "function() {
					xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsDefaultPlugin'}, {parameters: [{obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$aFormValues['project_id']."}]});
				}";
			}
			$elements[] = $add_obj;
		}

		$objResponse->script($this->tools->strip("$('#CMS_project_tree_".$aFormValues['project_id']."').xTree('update',".json_encode(
			array(
				"parent" => $aFormValues['parent_id'],
				"elements" => $elements
			)
		).")"));

		return $objResponse;
	}



	/* ---------------------------------------------------------------------------------------------------------------------------------- */


	function changeProjectLanguage( $aFormValues ) 
	{
		$objResponse = new ccdsResponse();
		
		$_SESSION['CMS']['lang'][$aFormValues['project_id']] = $aFormValues['lang_id'];
				
		if (is_array($aFormValues['opened']))
		{
			foreach ($aFormValues['opened'] as $opened)
			{
				$matches = array();
				if (1 == preg_match("/^navi_obj_([0-9].*)/",$opened,$matches))
				{
					$objResponse->loadcommands(
						$this->updateNaviEntry(array(
							'project_id' => $aFormValues['project_id'],
							'obj_parent_obj_id' => $matches[1],
							'parent_id' => $opened
						))
					);
				}
			}
		}
		$languages = array();
		$query = "SELECT languages.* FROM languages LEFT JOIN mn_project_language ON project_language_key_language_id=lang_id WHERE project_language_key_project_id='".addslashes($aFormValues['project_id'])."'";
		$res = $this->ccdSQL->query($query);
		while ($row = $this->ccdSQL->fetch_assoc($res)) $languages[]=$row;
		$this->smarty->assign("project_id",$aFormValues['project_id']);
		$this->smarty->assign("languages",$languages);
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_languages.tpl" );
		$objResponse->assign("project_languages_".$aFormValues['project_id'],"innerHTML",$sHtml);
		return $objResponse;
	}


	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function updateProjectNavi( $aFormValues )
	{

		$elements = array();;

		$objResponse = new ccdsResponse();
			
		$query = "SELECT * FROM projects WHERE project_id = '".$aFormValues['project_id']."'";
		$res = $this->ccdSQL->query($query);
		$project = $this->ccdSQL->fetch_assoc($res);
					
		$project['languages'] = array();
		$query = "SELECT * FROM mn_project_language LEFT JOIN languages ON project_language_key_language_id = lang_id WHERE project_language_key_project_id='".$project['project_id']."'";
		$reslang = $this->ccdSQL->query($query);
		while ($lang = $this->ccdSQL->fetch_assoc($reslang)) 
		{
			$project['languages'][] = $lang;
			if ($lang['lang_id'] == $_SESSION['CMS']['user_lang_id']) $_SESSION['CMS']['lang'][$project['project_id']] = $lang['lang_id'];
		}
		
		if ("" == $_SESSION['CMS']['lang'][$project['project_id']])
		{
			$lang = $project['languages'][0];
			$_SESSION['CMS']['lang'][$project['project_id']] = $lang['lang_id'];
		}
		$_SESSION['CMS']['user_lang_id']="";
		
		
		$query = "
							SELECT 
								* 
							FROM 
								obj_index 

							STRAIGHT_JOIN classes ON
								(obj_key_class_id = class_id)
							STRAIGHT_JOIN extensions ON
								(ext_id = class_key_ext_id)
							STRAIGHT_JOIN projects ON
								(project_id = ext_key_project_id)

							LEFT OUTER JOIN 
								obj_index_public ON 
								(objp_key_obj_id=obj_id AND objp_key_lang_id='".$_SESSION['CMS']['lang'][$aFormValues['project_id']]."')
			";
		if ( 1000 > $_SESSION['CMS']['user_level']) {
			$query .="			
			STRAIGHT_JOIN permissions_obj_index_2_user ON (
				obj_index_2_user_read = 1
				AND
				obj_id = obj_index_2_user_key_obj_id
				AND
				obj_index_2_user_key_user_id = '".$_SESSION['CMS']['user_id']."'
			)
			";
		}
		$query .= "

							WHERE 
								obj_parent_obj_id=0 
								AND obj_deleted=0
								AND obj_key_project_id='".$project['project_id']."'
						";

		
		$cres = $this->ccdSQL->query($query);
		$childs = array();
		while ( $obj = $this->ccdSQL->fetch_assoc( $cres )) {
			$disabled = $obj['objp_key_obj_id'] > 0 ? 0 : 1;
			$name = $this->tools->getObjectName( $obj['obj_id'], $_SESSION['CMS']['lang'][$project['project_id']], true );
			$obj = array_merge( $obj, $name );

			if ( file_exists( $this->config->get( 'EXTENSION_DIR' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png" ) )
			{
				$icon_src = $this->config->get( 'EXTENSION_WEB' ).$row['project_dir']."/".$row['ext_dir']."/".$row['class_dir']."/img/icon.png";
			}
			else
			{
				$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/house.png";
			}

			$elements[] = array(
				"id" => "navi_obj_".$obj['obj_id'],
				"name" => $obj['obj_name_name'],
				"icon" => $icon_src,
				"onNameClick" => "xajax.request({ccdsfun: 'ContentEditorOpen', ccdsclass: 'ccdsContentEditor'}, {parameters: [{obj_id: '".$obj['obj_id']."'}]});",
				"onOpen" => "function(parent_id) {xajax.request({ccdsfun: 'getNavi', ccdsclass: 'ccdsDefaultPlugin'}, {parameters: [{parent_id:parent_id,obj_parent_obj_id: '".$obj['obj_id']."',project_id:".$obj['obj_key_project_id']."}]});}",
				"folder" => true,
				"iconAttr" => array("title" => "id: ".$obj['obj_id']." Klasse: ".$obj['class_name']),
				"disabled" => $disabled
			);
		}
		

		$foo = array(&$elements);
		$this->modules->call('project_navi',$project,$foo);

		$this->smarty->assign( "project", $project );
		$sHtml = $this->smarty->fetch( $this->tpl_dir."project_navi.tpl" );
		$objResponse->assign( "CMS_project_".$project['project_id'], "innerHTML", $sHtml );
		
				
		$objResponse->script("$('#CMS_project_tree_".$project['project_id']."').xTree(".json_encode(array("animate" => false,"multipleSelect" => false, "elements" => $elements)).")");


		return $objResponse;
	}

	function UpdateProjectLanguages($aFormValues) {
		$objResponse = new ccdsResponse();
		
		$query = "SELECT languages.* FROM languages STRAIGHT_JOIN mn_project_language on (project_language_key_language_id = lang_id AND project_language_key_project_id='".addslashes($aFormValues['project_id'])."')";
		$res = $this->ccdSQL->query($query);
		$languages = array();
		while ($lang = $this->ccdSQL->fetch_assoc($res)) {
			$languages[] = $lang;
		}
		$this->smarty->assign("languages",$languages);
		$this->smarty->assign("project_id",$aFormValues['project_id']);
		
		$sHtml = $this->smarty->fetch($this->tpl_dir."project_languages.tpl");
		$objResponse->assign("project_languages_".$aFormValues['project_id'],"innerHTML",$sHtml);
		
		return $objResponse;
		
	}

	function check_login() {
		
		if ( is_numeric($_SESSION['CMS']['user_id']) && $_SESSION['CMS']['user_id'] > 0 ) return true;
		
		if ( isset($_COOKIE['CCDS3_USER']{10}) && !empty($_COOKIE['CCDS3_USER']) ) 
		{
			$query = "SELECT * FROM `user` WHERE user_cookie='".$_COOKIE['CCDS3_USER']."'";
			$res = $this->ccdSQL->query($query);
			if (!$row = $this->ccdSQL->fetch_assoc($res)) return false;

			$this->tools->init_session($row);
			return true;
		}
		
		
		return false;
		
	}


	/* ---------------------------------------------------------------------------------------------------------------------------------- */

	function run()
	{
		if ($this->check_login())
		{
			$onloadScript = "xajax.request({ccdsfun:'initApp',ccdsclass:'ccdsDefaultPlugin'},{parameters:[{}]});";
		}
		else
		{
			$onloadScript = "xajax.request({ccdsfun:'openLogin',ccdsclass:'ccdsDefaultPlugin'},{parameters:[{}]});";
		}
		$xajax = ccdsXajax::getInstance();
		$xajax->configure( "javascript URI", $this->config->get( 'LIBRARY_WEB' )."xajax/" );
		
		$xajax->registerFunction( "customFunction" );
		$xajax->registerFunction( "customFormFunction" );

		$this->smarty->assign( 'xajax_javascript', $xajax->getJavascript() );

		$this->smarty->assign( "onLoadScript", $onloadScript );
		$this->smarty->display( $this->tpl_dir."index.tpl", "" );
	}
/* ---------------------------------------------------------------------------------------------------------------------------------- */

}
